package com.petalsgroup.petals.web.servlet.InformServlet;

import com.petalsgroup.petals.dao.bannedTime.BannedTimeDao;
import com.petalsgroup.petals.dao.bannedTime.impl.BannedTimeDaoImpl;
import com.petalsgroup.petals.pojo.BannedTimeDO;
import com.petalsgroup.petals.service.Inform.InformService;
import com.petalsgroup.petals.service.Inform.impl.InformServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

@WebListener
public class BannedResolveListener implements ServletContextListener{
    InformService is=new InformServiceImpl();
    BannedTimeDao btd = new BannedTimeDaoImpl();
    @Override//服务器开启后执行的方法
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("进入了");
        //1.开启发送生日祝福的周期性任务
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("进入run");
                //每天判断一次是否有用户达到解禁时间
                List<BannedTimeDO> times= is.findAllOpenTimeList();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date now =new Date();
                System.out.println(now);
                for (BannedTimeDO time:times) {
                    System.out.println(time.getOpen_time());
                    System.out.println(now.compareTo(time.getOpen_time()));
                    System.out.println("进入循环");
                    if (now.compareTo(time.getOpen_time())>=0){
                        System.out.println("进入判断");
                        is.rosolveUserById(time.getUid());
                        btd.deletBannedTimeById(time.getUid());
                    }
                }
            }
        },3000,60l*60*24*1000);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }


}
